from django.contrib import admin
from .models import (
    CommissionRule, CommissionRecord, 
    Invoice, FinancialReport, PaymentTransaction
)


@admin.register(CommissionRule)
class CommissionRuleAdmin(admin.ModelAdmin):
    list_display = ['name', 'commission_type', 'commission_rate', 'min_amount', 'is_active', 'created_at']
    list_filter = ['commission_type', 'is_active', 'created_at']
    search_fields = ['name', 'description']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'description', 'commission_type')
        }),
        ('佣金设置', {
            'fields': ('commission_rate', 'fixed_amount', 'min_amount', 'max_amount')
        }),
        ('状态', {
            'fields': ('is_active', 'valid_from', 'valid_to')
        })
    )


@admin.register(CommissionRecord)
class CommissionRecordAdmin(admin.ModelAdmin):
    list_display = ['order', 'commission_amount', 'status', 'created_at']
    list_filter = ['status', 'created_at']
    search_fields = ['order__order_number']
    readonly_fields = ['order', 'commission_amount', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('order', 'commission_amount', 'status')
        }),
        ('时间', {
            'fields': ('created_at', 'paid_at'),
            'classes': ('collapse',)
        })
    )


@admin.register(Invoice)
class InvoiceAdmin(admin.ModelAdmin):
    list_display = ['invoice_number', 'order', 'amount', 'status', 'issue_date', 'due_date']
    list_filter = ['status', 'invoice_type', 'issue_date']
    search_fields = ['invoice_number', 'order__order_number', 'recipient_name']
    readonly_fields = ['invoice_number', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('invoice_number', 'order', 'invoice_type', 'status')
        }),
        ('金额信息', {
            'fields': ('amount', 'tax_amount', 'total_amount')
        }),
        ('收票人信息', {
            'fields': ('recipient_name', 'recipient_email', 'recipient_address')
        }),
        ('时间信息', {
            'fields': ('issue_date', 'due_date', 'paid_date')
        }),
        ('文件', {
            'fields': ('invoice_file',),
            'classes': ('collapse',)
        })
    )


@admin.register(FinancialReport)
class FinancialReportAdmin(admin.ModelAdmin):
    list_display = ['report_type', 'start_date', 'end_date', 'total_revenue', 'total_expenses', 'net_profit', 'created_at']
    list_filter = ['report_type', 'created_at']
    search_fields = ['report_type', 'description']
    readonly_fields = ['created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('report_type', 'start_date', 'end_date', 'description')
        }),
        ('收入', {
            'fields': ('total_revenue', 'book_sales', 'shipping_income', 'other_income')
        }),
        ('支出', {
            'fields': ('total_expenses', 'book_costs', 'shipping_costs', 'commissions', 'operational_costs')
        }),
        ('利润', {
            'fields': ('net_profit',)
        }),
        ('文件', {
            'fields': ('report_file',),
            'classes': ('collapse',)
        })
    )


@admin.register(PaymentTransaction)
class PaymentTransactionAdmin(admin.ModelAdmin):
    list_display = ['transaction_id', 'order', 'payment_method', 'amount', 'status', 'created_at']
    list_filter = ['status', 'payment_method', 'created_at']
    search_fields = ['transaction_id', 'order__order_number', 'gateway_transaction_id']
    readonly_fields = ['transaction_id', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('transaction_id', 'order', 'payment_method', 'status')
        }),
        ('金额信息', {
            'fields': ('amount', 'currency', 'fee_amount', 'net_amount')
        }),
        ('网关信息', {
            'fields': ('gateway', 'gateway_transaction_id', 'gateway_response'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at', 'completed_at', 'failed_at'),
            'classes': ('collapse',)
        })
    )
